package com.ta.gulimall.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ta.gulimall.product.dao.SkuImagesDao;
import com.ta.gulimall.product.service.SkuImagesService;
import com.ta.gulimall.product.view.entity.SkuImagesEntity;
import com.ta.gulimall.product.view.vo.Images;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


@Service("skuImagesService")
public class SkuImagesServiceImpl extends ServiceImpl<SkuImagesDao, SkuImagesEntity> implements SkuImagesService {

    @Override
    public void saveSkuImages(Long skuId, List<Images> images) {
        images = Optional.ofNullable(images).orElse(new ArrayList<>());
        List<SkuImagesEntity> imagess = images.stream()
                .filter(f -> ObjectUtil.isNotEmpty(f.getImgUrl()))
                .map(m -> BeanUtil.copyProperties(m, SkuImagesEntity.class).setSkuId(skuId)).collect(Collectors.toList());

        saveBatch(imagess);
    }

    @Override
    public List<SkuImagesEntity> listBySkuId(Long skuId) {
        LambdaQueryWrapper<SkuImagesEntity> wrapper = new LambdaQueryWrapper<SkuImagesEntity>()
                .eq(SkuImagesEntity::getSkuId, skuId);
        List<SkuImagesEntity> skuImagesEntities = list(wrapper);

        return skuImagesEntities;
    }
}